import 'package:tobias/tobias.dart';
import 'package:xsdk_flutter/xsdk/utils/json/x_utils_json.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';

/*
支付宝: 支付,分享  android iOS
https://pub-web.flutter-io.cn/packages/tobias/install
https://github.com/OpenFlutter/tobias/blob/master/README_CN.md
https://opendocs.alipay.com/open/204/105051/

dependencies:
  tobias: ^3.3.2
import 'package:tobias/tobias.dart';

其他第三方alipay_kit:
https://pub-web.flutter-io.cn/packages/alipay_kit

 */
abstract class XAlipayUtils{

  static Tobias? _tobias;

  //1.初始化
  /*
  返回值是一个包含支付宝支付结果的map。其中还包含了一个额外的 platform字段， 它的值为 iOS 或 android。

注意:Tobias 使用的是 pay_V2.
   */
  static void init(){
    _tobias ??= Tobias();
  }

  static void pay(
      String order
      ) async
  {

    bool? isAliPayInstalled= await _tobias?.isAliPayInstalled;

    if(isAliPayInstalled==true){
      String? aliPayVersion=await _tobias?.aliPayVersion;
      XLogUtils.printLog('---aliPayVersion=$aliPayVersion');

      Map<dynamic,dynamic>? res_aliPay=await _tobias?.pay(order);
      XLogUtils.printLog('---res_aliPay=${XJsonUtils.obj2Json(res_aliPay)}');

      dynamic resultStatus=res_aliPay?['resultStatus'];
      if(resultStatus=='9000'){
        XLogUtils.printLog('---res_aliPay,支付成功');
      }else{
        XLogUtils.printLog('---res_aliPay,支付失败');
      }
    }

  }

  //app通过支付宝认证登录
  static void loginAuth(
      String auth
      )async
  {

    bool? isAliPayInstalled= await _tobias?.isAliPayInstalled;

    if(isAliPayInstalled==true){

      Map<dynamic,dynamic>? res_auth=await _tobias?.auth(auth);
      XLogUtils.printLog('---res_aliPay=${XJsonUtils.obj2Json(res_auth)}');

      dynamic resultStatus=res_auth?['resultStatus'];
      if(resultStatus=='9000'){
        XLogUtils.printLog('---res_aliPay,认证成功');
      }else{
        XLogUtils.printLog('---res_aliPay,认证失败');
      }
    }

  }

}

